#coding=utf-8
'''
Created on May 11, 2016
@author: wangpeng
'''

import unittest
from appium import webdriver
from selenium.webdriver.common.by import By   #用于匹配某个值是否出现，智能等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import common.config
import common.logging_save_huawei_honor6
import common.desired_setting_huawei_honor6
import threading
import re
import common.Create_Log_Folder
import os
import os.path
import common.logging_save_huawei_honor6
import serial
#import common.save_serial_log
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import subprocess

class Test(unittest.TestCase):
    def setUp(self):
        os.system('taskkill /f /im adb.exe')
        common.logging_save_huawei_honor6.logging.info("app测试开始，正在开始启动app")
        print time.ctime(),"app测试开始，正在开始启动app"
        self.begin_time_for_serial = time.strftime("%y%m%d_%H:%M:%S", time.localtime()) 
        self.save_log = common.Create_Log_Folder.Create("\\huawei_honor6\\lock_unlock")    #建立report保存路径
        common.logging_save_huawei_honor6.logging.info("进行锁屏和解锁屏幕测试")
        print time.ctime(),"进行锁屏和解锁屏幕测试"
        sys.path.append('\adbcommand')  #增加adb log
        self.test = subprocess.Popen(r'E:\eclipse\huawei_rader\all_mobile\testcase\huawei_honor6\adbcommond\lock_unlock.bat') #导入adb
        time.sleep(1)
        
        #开启appium设置
        self.desired_setting = common.desired_setting_huawei_honor6.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_setting)
        common.logging_save_huawei_honor6.logging.info("app连接BLE中，70秒后再做判定。。。")
        print time.ctime(),"app连接BLE中，70秒再做判定。。。"
        time.sleep(70)
        try:
            self.driver.find_element_by_id("com.huawei.smallRadar:id/sure").click()
            time.sleep(3)
            common.logging_save_huawei_honor6.logging.info("close弹出框")
            print time.ctime(),"close弹出框"

        except:
            pass

        
    def tearDown(self):
        self.driver.quit()
        time.sleep(1)
        self.test.kill()
        os.system('taskkill /f /im adb.exe')
        print '关闭adb log'
        time.sleep(10)
        print 'finished'
        
    def test_play(self):
        u"""测试步骤：进入到TVOC界面，锁屏，然后按power恢复，重复500次   测试结果：观察：   1.可以搜索到TVOC页面的某个标志"""
        savenumber = 1
        save_pic = os.path.join(self.save_log+str(savenumber)+'.png')
        #判断是否进入主界面
        try:
            WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.ID,common.config.wendu()))) #确认是否进入到主界面                       
            common.logging_save_huawei_honor6.logging.info("经过确认，已经进入到主菜单页面")
            print time.ctime(),"经过确认，已经进入到主菜单页面"
            time.sleep(3)
        except:
            self.driver.get_screenshot_as_file(save_pic)
        
        time.sleep(1)
        #检测是否出现异常
        testtime = 1
        while(testtime<=50):
            self.driver.lock(20)   #锁屏五秒钟
            common.logging_save_huawei_honor6.logging.info("锁屏5秒")
            print time.ctime(),"锁屏5秒"
            time.sleep(5)
            
            self.driver.keyevent(26)  #开机唤醒屏幕
            common.logging_save_huawei_honor6.logging.info("解锁屏幕")
            print time.ctime(),"解锁屏幕"
            time.sleep(2)
            self.driver.swipe(700, 700, 200, 700, 1000)
            time.sleep(2)
            
            def validate_ok():
                try:
                    WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.ID,common.config.wendu()))) #确认是否进入到主界面                       
                    common.logging_save_huawei_honor6.logging.info("经过确认，已经进入到TVOC页面,测试次数为"+str(testtime+1)+"次")
                    print time.ctime(),"经过确认，已经进入到主菜单页面"+str(testtime+1)+"次"
                    time.sleep(3)
                    a = 1
                    return a
                
                except:
                    self.driver.get_screenshot_as_file(save_pic)
                    a = 0
                    return a
                    self.fail("经过确认，不可以正常返回到TVOC主材带界面")
                    
            self.assertEqual(validate_ok(), 1, "发现连接断开现象，请确认问题所在")
            
            common.logging_save_huawei_honor6.logging.info("经过确认，蓝牙正常连接成功")
            print time.ctime(),"经过确认，蓝牙正常连接成功"
            testtime = testtime + 1

if __name__ == "__main__":
    unittest.main()